package com.liefox.eduucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liefox.commonutils.JwtUtils;
import com.liefox.commonutils.MD5;
import com.liefox.commonutils.exceptionhandler.GuliException;
import com.liefox.eduucenter.entity.UcenterMember;
import com.liefox.eduucenter.entity.vo.RegisterVo;
import com.liefox.eduucenter.mapper.UcenterMemberMapper;
import com.liefox.eduucenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author liefox
 * @since 2021-07-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(UcenterMember ucenterMember) {
        //手机号和密码做登录
        //1、获取手机号和密码
        String email = ucenterMember.getEmail();
        String password = ucenterMember.getPassword();

        //2、如过邮箱和密码，直接返回登录失败
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "邮箱和密码为空");
        }
        //判断邮箱是否正确
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        UcenterMember emailMember = baseMapper.selectOne(queryWrapper);
        if (emailMember == null) {
            throw new GuliException(20001, "邮箱不存在");
        }

        //判断密码是否相等
        //数据库密码进行加密，不能直接对比
        //MD5对密码进行加密，再与数据库进行比较
        String password1 = emailMember.getPassword();
        if (!MD5.encrypt(password).equals(password1)) {
            throw new GuliException(20001, "密码错误");
        }

        //判断用户是否被禁用
        if (emailMember.getIsDisabled()) {
            throw new GuliException(20001, "用户被禁用登录失败");
        }

        //登录成功
        //按照jwt生产token返回
        String token = JwtUtils.getJwtToken(emailMember.getId(), emailMember.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        final String email = registerVo.getEmail();
        final String password = registerVo.getPassword();
        final String nickname = registerVo.getNickname();
        final String code = registerVo.getCode();

//        非空判断
        if (StringUtils.isEmpty(email)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "注册失败!");
        }

        //      判断邮箱是否已经存在
        final QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        final Integer one = baseMapper.selectCount(wrapper);
        if (one > 0) {
            throw new GuliException(20001, "该邮箱以注册");
        }

//      判断验证码
        final String res = redisTemplate.opsForValue().get(email);
        if (!res.equals(code)) {
            throw new GuliException(20001, "验证码错误!");
        }


        //添加到数据库
        final UcenterMember member = new UcenterMember();
        member.setEmail(email);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false);
        member.setAvatar("https://cxq21.gitee.io/images/LIEFox.png");

        baseMapper.insert(member);

    }

    @Override
    public Integer countRegister(String day) {
        Integer count = baseMapper.countRegister(day);
        return count;
    }
}
